package com.hinacom.pix.receiver;

import ca.uhn.hl7v2.AcknowledgmentCode;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.Location;
import ca.uhn.hl7v2.app.Application;
import ca.uhn.hl7v2.app.ApplicationException;
import ca.uhn.hl7v2.app.SimpleServer;
import ca.uhn.hl7v2.model.Group;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.MessageVisitor;
import ca.uhn.hl7v2.model.Structure;
import ca.uhn.hl7v2.model.v231.message.ADT_A31;
import ca.uhn.hl7v2.model.v25.message.*;
import ca.uhn.hl7v2.model.v25.segment.MSH;
import ca.uhn.hl7v2.parser.Parser;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.validation.ValidationContext;
import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * Created by fyu on 2016/11/25.
 */
public class PIXComsumerHandler implements Application {
    private Properties properties;
    private static Logger logger = Logger.getLogger(PIXComsumerHandler.class);

    public PIXComsumerHandler(Properties properties)
    {
        logger.debug("set properties");
        this.properties = properties;
    }

    @Override
    public Message processMessage(Message message) throws ApplicationException, HL7Exception {
        Message result = null;
        try {
            logger.debug("Recciver message");
            String input = message.encode();
            logger.info("input message: " + input);


            logger.debug("get control id");
            ADT_A05 a05 = (ADT_A05) message;
            String controlId = a05.getMSH().getMsh10_MessageControlID().getValue();

            //for demo purposes, we just declare a literal message string
            logger.debug("get sending application and facility from properties");
            String sendApplication = this.properties.getProperty("sending-application-name");
            String sendFacility = this.properties.getProperty("sending-facility-name");
            ;
            logger.debug("get receiving application and facility from message");
            String receivingApplication = a05.getMSH().getMsh3_SendingApplication().encode();
            String receivingFacility = a05.getMSH().getMsh4_SendingFacility().encode();

            logger.debug("generate message");
            String ackMessageString
                    = String.format("MSH|^~\\&|%s|%s|%s|%s|20161125164240.704+0800||ACK^A31^ACK|%s|P|2.5\rMSA|AA|%s|\r", sendApplication, sendFacility, receivingApplication, receivingFacility, controlId, controlId);

            //instantiate a PipeParser, which handles the "traditional encoding"
            logger.debug("convert message");
            PipeParser pipeParser = new PipeParser();
            logger.info("output message: " + ackMessageString);

            //parse the message string into a Message object
            result = pipeParser.parse(ackMessageString);
            //instantiate an XML parser
        } catch (Exception e) {
            logger.error("parser message error:", e);
        }
        return result;
    }

    @Override
    public boolean canProcess(Message message) {
        logger.debug("message type:" + message.getClass().getName());

        if (message instanceof ADT_A05) {
            logger.debug("message type can be handled");
            return true;
        } else {
            logger.debug("message type can not be handled");
            return false;
        }
    }

}
